//
//  WelcomeViewController.swift
//  SinaWeibo
//
//  Created by Apple on 15/11/13.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class WelcomeViewController: UIViewController {
    //注:不要再viewDidLoad方法中去做动画,不然图片会从左上角加载
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
//        stratAnimation()
    }
    //执行动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        stratAnimation()
    }
    //设置背景
    override func loadView() {
        view = backImage
    }
    
    //动画效果
    private func stratAnimation(){
        //改变约束
        self.iconImage.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(-self.view.bounds.height + 180)
        }
        //由远及近效果
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.6, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
//                        print("OK")
                        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewController, object: nil)
                })
        }
    }
    
    
    //MARK:添加子视图
    private func setupUI(){
        
        view.addSubview(iconImage)
        view.addSubview(welcomeLabel)
        //设置布局
        iconImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_bottom).offset(20)
            make.centerX.equalTo(view.snp_centerX)
        }
        iconImage.sd_setImageWithURL(UserAccountViewModel().headURL, placeholderImage: UIImage(named: "avatar_default_big"))
        iconImage.layer.cornerRadius = 45
        iconImage.layer.masksToBounds = true
        
    }
    //懒加载视图
    private lazy var iconImage:UIImageView = UIImageView()
    private lazy var welcomeLabel: UILabel = {
        let l = UILabel()
        l.textColor = UIColor.lightGrayColor()
        l.font = UIFont.systemFontOfSize(19)
        l.textAlignment = .Center
        l.text = (UserAccountViewModel().userName ?? "") + "  欢迎归来"
        l.alpha = 0
        l.sizeToFit()
        return l
        }()
    private lazy var backImage:UIImageView = UIImageView(image: UIImage(named: "ad_background"))

}
